<!-- 物流公司信息 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="isCert">
        <el-select v-model="queryParams.isCert" placeholder="请选择认证状态" clearable size="small">
          <el-option
            v-for="dict in isCertOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option label="正常" value="1" />
          <el-option label="禁用" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tws:company:export']"
        >导出</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-badge :value="isCertNum">
          <el-button
            type="success"
            plain
            icon="el-icon-coordinate"
            size="mini"
            @click="companyCert"
            v-hasPermi="['tws:company:edit']"
          >物流公司审核</el-button>
        </el-badge>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="110">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isCert == $store.state.constants.isCert_yrz"
            style="color: #67c23a;"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tws:company:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.isCert == $store.state.constants.isCert_yrz"
            style="color: red;"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tws:company:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center">
        <span slot="header">
          公司名称
          <el-tooltip effect="dark" placement="top" content="点击公司名称可查看详情">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </span>
        <template slot-scope="scope">
          <span style="color: #1890ff; cursor: pointer;" @click="handleView(scope.row)">{{ scope.row.companyName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="纳税人识别码" align="center" prop="taxNo" />
      <el-table-column label="道路运输经营许可证号" align="center" prop="licenseNo" />
      <el-table-column label="公司电话" align="center" prop="tel" />
      <el-table-column label="注册地址" align="center" prop="address" show-overflow-tooltip />
      <el-table-column label="法人" align="center" prop="legal" />
      <el-table-column label="联系人" align="center" prop="linker" />
      <el-table-column label="联系人电话" align="center" prop="linktel" />
      <el-table-column label="认证状态" align="center" prop="isCert" :formatter="isCertFormat" />
      <el-table-column label="状态" align="center" width="140">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="2"
            active-text="正常"
            inactive-text="禁用"
            @change="changeState(scope.row)" :disabled="scope.row.isCert !== $store.state.constants.isCert_yrz">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改物流公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="false">
      <company-form :mode="mode" :companyId="companyId" @close="close" />
    </el-dialog>

  </div>
</template>

<script>
import { listCompany, delCompany, getCertNum, stateChange } from "@/api/tws/company";
import CompanyForm from '@/components/CompanyForm/index';

export default {
  name: "Company",
  components: {
    CompanyForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      mode: '3',
      companyId: null,
      // 审核计数
      isCertNum: 0,
      // 物流公司表格数据
      companyList: [],
      // 认证状态字典
      isCertOptions: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        companyName: null,
        shortCode: null,
        taxNo: null,
        licenseNo: null,
        tel: null,
        address: null,
        legal: null,
        legaltel: null,
        linker: null,
        linktel: null,
        bank: null,
        bankcard: null,
        businessLicense: null,
        roadLicense: null,
        idCardImg1: null,
        idCardImg2: null,
        isCert: null,
        state: null,
        tenantId: null
      }
    };
  },
  created() {
    this.getDict();
    this.getList();
    this.getCertNum();
  },
  methods: {
    /** 查询字典 */
    getDict() {
      // 认证状态字典
      this.getDicts("is_cert").then(response => {
        this.isCertOptions = response.data;
      });
    },
    /** 查询物流公司列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询待审核物流公司数量 */
    getCertNum() {
      getCertNum().then(res => {
        this.isCertNum = res.data;
      })
    },
    // 认证状态字典翻译
    isCertFormat(row, column) {
      return this.selectDictLabel(this.isCertOptions, row.isCert);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 停启用
    changeState(row) {
      this.$confirm('确定要' + (row.state == "1" ? '启用' : '禁用') + '该物流公司吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        let obj = {
          id: row.id,
          state: row.state
        }
        return stateChange(obj);
      }).then(() => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(err => {
        this.getList();
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id;
      this.open = true;
      this.title = "修改物流公司信息";
      this.mode = '';
      this.companyId = null;
      setTimeout(() => {
        this.mode = '2';  // 修改模式
        this.companyId = id;
      })
    },
    // 查看详情
    handleView(row) {
      const id = row.id;
      this.open = true;
      this.title = "物流公司详情";
      this.mode = '';
      this.companyId = null;
      setTimeout(() => {
        this.mode = '3';  // 查看模式
        this.companyId = id;
      })
    },
    close() {
      this.open = false;
      this.getList();
    },
    // 跳转物流公司审核页面
    companyCert() {
      this.$router.push({ path: 'companyCert', replace: true });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物流公司 "' + row.companyName + '" ?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delCompany(ids);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(err => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/company/export', {
        ...this.queryParams
      }, `物流公司.xlsx`)
    }
  }
};
</script>
